package elements 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import gs.easing.Cubic;
	import gs.TweenLite;
	import util.TextUtils;
	
	/**
	 * 
	 * @author Kasoi
	 */
	public class TextBlock extends Sprite 
	{
		
		////////////////////////////////////////////////////////////////////
		// Public properties
		////////////////////////////////////////////////////////////////////
		
		public function get showPhase() : Number 
		{
			return this._showPhase;
		}
		
		public function set showPhase(value: Number) : void 
		{
			this._showPhase = value;
			this.field.alpha = value;
			if(this.useScroller) this.scroller.alpha = value;
			
			var v: Number = 1 - value;
			//var f: BlurFilter = new BlurFilter(5 * v, 5 * v, 2);
			var color: Number = Number(this._field.defaultTextFormat.color);
			var f: GlowFilter = new GlowFilter(color, v, 10 * v, 10 * v,
				2, 2);
			this.filters = [f];
			if (value == 0) this.filters = null;
		}
		
		public function get field() : TextField 
		{
			return this._field;
		}
		
		public function get code() : String 
		{ 
			return this._code; 
		}
		
		public function get isShowing() : Boolean 
		{ 
			return this._isShowing; 
		}
		
		////////////////////////////////////////////////////////////////////
		// Private properties
		////////////////////////////////////////////////////////////////////
		
		private var _field: TextField;
		
		private var _showPhase: Number = 0;
		
		private var scroller: Scroller;
		
		private var useScroller: Boolean;
		
		private var _code: String;
		
		private var _isShowing: Boolean = false;
		
		////////////////////////////////////////////////////////////////////
		// Public methods
		////////////////////////////////////////////////////////////////////
		
		public function TextBlock(code: String, width: Number = 300,
			height: Number = 200, format: TextFormat = null, isJustify: Boolean = true,
			leading:Number = 0, useScroller: Boolean = true) 
		{
			this.useScroller = useScroller;
			if (format == null) 
			{
				format = new TextFormat(Lib.helveticaNeueBoldFont.fontName,
					10, 0xffffff);
				format.leading = 2;
			}
			
			if (isJustify) format.align = TextFormatAlign.JUSTIFY;
			if(!leading) {
				format.leading = (Number(format.size) - 9) * 1.5;
			}
			else 
			{
				format.leading = leading;
			}
			this._field = TextUtils.makeMultiLine(Texts.getText(code), format,
				width, NaN);
			this._field.condenseWhite = true;
			this._field.htmlText = Texts.getText(code);
			this._field.height = this._field.textHeight + 8 + format.leading;
			this._field.mouseWheelEnabled = false;
			this._field.selectable = true;
			this._field.addEventListener(Event.SCROLL, this.field_onScroll);
			
			if (code == '') this._field.text = '';
			
			//this.graphics.beginFill(0xff00ff, 0.3);
			//this.graphics.drawRect(0, 0, width, height);
			//this.graphics.endFill();
			
			if (this.useScroller)
			{
				this.scroller = new Scroller(this.field, height);
				//this.scroller = new Scroller(new Sprite(), height);
				//this.addChild(this._field);
				this.addChild(this.scroller);
			}
			else
			{
				this.addChild(this._field);
			}
			
			this.showPhase = 0;
		}
		
		public function show(delay: Number = 0) : void 
		{
			if (this._isShowing == true) return;
			this._isShowing = true;
			TweenLite.to(this, 1, {showPhase: 1, delay: delay, ease: Cubic.easeOut } );
		}
		
		public function hide(delay: Number = 0) : void 
		{
			this._isShowing = false;
			TweenLite.to(this, 0.5, {showPhase: 0, delay: delay, ease: Cubic.easeOut } );
		}
		
		public function update() : void 
		{
			this._field.height = this._field.textHeight + 8 + this._field.defaultTextFormat.leading;
			if (this.useScroller) this.scroller.update();
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function changeTexts(code: String) : void 
		{
			this._code = code;
			if (this._showPhase <= 0.05)
			{
				this.switchTexts();
			}
			else
			{
				this.hide();
				TweenLite.to(this._field, 0.5, { onComplete: this.switchTexts } );
			}
		}
		
		////////////////////////////////////////////////////////////////////
		// Private methods
		////////////////////////////////////////////////////////////////////
		
		private function switchTexts() : void 
		{
			this._field.htmlText = Texts.getText(this._code);
			this.update();
			this.show();
		}
		
		////////////////////////////////////////////////////////////////////
		// Listeners
		////////////////////////////////////////////////////////////////////
		
		private function field_onScroll(e: Event) : void 
		{
			//e.preventDefault();
			//this._field.scrollV = 0;
		}
	}
	
}